﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;

/// <summary>
/// EmailSender 的摘要说明
/// </summary>
public class EmailSender
{
    private string mailFromAddress = ConfigurationManager.AppSettings["MailFromAddress"];
    private bool useSsl = bool.Parse(ConfigurationManager.AppSettings["UseSsl"]);
    private string userName = ConfigurationManager.AppSettings["UserName"];
    private string password = ConfigurationManager.AppSettings["Password"];
    private string serverName = ConfigurationManager.AppSettings["ServerName"];
    private int serverPort = int.Parse(ConfigurationManager.AppSettings["ServerPort"]);
    private string findPassword;  //重置后的密码
    private string mailToAddress = "";  //收件人邮箱
    public EmailSender(string address, string pwd)
    {
        mailToAddress = address;
        findPassword = pwd;
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public void Send()
    {
        //新建SmtpClient类实例smtpClient对象，using语句块结束时释放smtpClient对象
        using (var smtpClient = new SmtpClient())
        {
            //设置是否使用SSL协议连接
            smtpClient.EnableSsl = useSsl;
            //设置SMTP服务器名
            smtpClient.Host = serverName;
            //设置SMTP服务器的端口号
            smtpClient.Port = serverPort;
            //设置SMTP服务器发送邮件的凭据（用户名和授权码)
            smtpClient.Credentials = new NetworkCredential(userName, password);
            string body = "您登录的密码已重置为：" + findPassword;
            MailMessage mailMessage = new MailMessage(
                                   mailFromAddress,   // 发件人邮箱
                                   mailToAddress,     // 收件人邮箱
                                   "用户密码重置",  // 电子邮件主题
                                   body);  // 电子邮件内容
                                           //调用smtpClient对象的Send()方法发送邮件
            smtpClient.Send(mailMessage);
        }
    }
}